package org.codehaus.janino.util.resource;

import org.codehaus.janino.Resource;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

/**
 * Representation of a resource that is a {@link java.io.File}.
 */
public class FileResource implements Resource {

    private FileConnection conn;

    public FileResource(String url) {
        this.url = url;
    }

    // Implement "Resource".
    public String getFileName() {
        return this.url;
    }

    public InputStream open() throws IOException {
        return getFile().openDataInputStream();
    }

    public long lastModified() {
        return this.getFile().lastModified();
    }

    public FileConnection getFile() {
        if (conn == null) {
            try {
                conn = (FileConnection) Connector.open(url);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return conn;
    }

    public String toString() {
        return this.getFileName();
    }
    private final String url;
}
